<?php


namespace SidToken\Utils;


use Common\Model\MemberModel;
use SidToken\Model\SidTransferLogModel;

class MemberWalletUtil
{
    /**
     * @param $uid
     * @return array
     */
    function getWallet($uid)
    {
        //$this->syncBalance($uid);

        $userInfo = query_user(['score1', 'score2'], $uid);
        if (!$userInfo) {
            return [
                'usdt' => 0,
                'sid' => 0
            ];
        }
        return [
            'usdt' => floatval($userInfo['score2']),
            'sid' => floatval($userInfo['score1'])
        ];
    }

    /**
     * @param $uids
     * @param $type
     * inc|dec
     * @param $from
     * @param $to
     * @param $txid
     * @param $amount
     * @param string $reason
     * @return bool|int|string
     */
    function changeSid($uids, $type, $from, $to, $txid, $amount, $reason = '', $status = 1, $admin = false)
    {
        $uids = is_array($uids) ? $uids : explode(',', $uids);
        $model = new MemberModel();
        switch ($type) {
            case 'inc':
                $amount = abs($amount);
                $res = $model->where(array('uid' => array('in', $uids)))->setInc('score1', $amount);
                break;
            case 'dec':
                $amount = abs($amount);
                $res = $model->where(array('uid' => array('in', $uids)))->setDec('score1', $amount);
                break;
            default:
                $res = false;
                break;
        }

        $TransferLogModel = new SidTransferLogModel();
        $TransferLogModel->addTransferLogs($uids, "SID", $type, $from, $to, $txid, $amount, $reason, $status);

        foreach ($uids as $val) {
            if ($admin) {
                remoteCleanQueryUser($val, 'score1');
            } else {
                clean_query_user_cache($val, 'score1');
            }

        }
        unset($val);
        return $res;
    }

    function changeUsdt($uids, $type, $from, $to, $txid, $amount, $reason = '', $status = 1, $admin = false)
    {
        //校验余额
        if ($type == 'dec' && !is_array($uids)) {
            $balance = get_user_usdt_balance($uids);
            if ($balance - abs($amount) < 0) {
                return false;
            }
        }
        $uids = is_array($uids) ? $uids : explode(',', $uids);
        $model = new MemberModel();
        switch ($type) {
            case 'inc':
                $amount = abs($amount);
                $res = $model->where(array('uid' => array('in', $uids)))->setInc('score2', $amount);
                break;
            case 'dec':
                $amount = abs($amount);
                $res = $model->where(array('uid' => array('in', $uids)))->setDec('score2', $amount);
                break;
            default:
                $res = false;
                break;
        }

        $TransferLogModel = new SidTransferLogModel();
        $TransferLogModel->addTransferLogs($uids, "USDT", $type, $from, $to, $txid, $amount, $reason, $status);

        foreach ($uids as $val) {
            if ($admin) {
                remoteCleanQueryUser($val, 'score2');
            } else {
                clean_query_user_cache($val, 'score2');
            }
        }
        unset($val);
        return $res;
    }
}